package com.inspur.zhangpeixia.goods.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.inspur.zhangpeixia.goods.entity.Category;
import com.inspur.zhangpeixia.goods.service.CategoryService;
import com.inspur.zhangpeixia.goods.service.impl.CategoryServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
@WebServlet(name = "categoryServlet",value="/category")
public class CategoryServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method=req.getParameter("method");
        if(method.equals("list")){
            listCategory(req,resp);
        }else if(method.equals("add")){
            addCategory(req,resp);
        }else if(method.equals("update")){
            updateCategory(req,resp);
        }else if(method.equals("del")){
            deleteCategory(req,resp);
        }else if(method.equals("findAllCategory")){
            findAllCategory(req,resp);
        }
    }

    private CategoryService categoryService=new CategoryServiceImpl();
    //开发查询方法
    protected void listCategory(HttpServletRequest req, HttpServletResponse resp) throws ServletException,IOException {
        String kw = req.getParameter("kw");//获取条件查询参数
        if (kw == null) {
            kw = "";
        }
        //转码，解决乱码
        kw = new String(kw.getBytes("ISO-8859-1"), "UTF-8");
//调用service中的ListCategory
        List<Category> list = categoryService.listCategory(kw);
        System.out.println(list);
        req.setAttribute("list", list);
        req.getRequestDispatcher("/pages/category/category_list.jsp").forward(req, resp);//转发到列表页面

    }

    //开发添加的方法
    protected void addCategory(HttpServletRequest req, HttpServletResponse resp) throws ServletException,IOException{
        String categoryName = req.getParameter("categoryName");
        categoryName = new String(categoryName.getBytes("ISO-8859-1"),"UTF-8");
        Category category = new Category();
        category.setCategoryName(categoryName);
        int result = categoryService.addCategory(category);
        if(result>0){
            listCategory(req,resp);
        }
    }
    //开发删除的方法
    protected void updateCategory(HttpServletRequest req, HttpServletResponse resp) throws ServletException,IOException{
        int categoryId = Integer.parseInt(req.getParameter("categoryId"));
        String categoryName = req.getParameter("categoryName");
        categoryName = new String(categoryName.getBytes("ISO-8859-1"),"UTF-8");
        Category category = new Category();
        category.setCategoryId(categoryId);
        category.setCategoryName(categoryName);
        int result = categoryService.updateCategory(category);
        if(result>0){
            listCategory(req,resp);
        }
    }
    protected void deleteCategory(HttpServletRequest req, HttpServletResponse resp)throws ServletException,IOException {

        String id = req.getParameter("id");
        int result = categoryService.deleteCategory(Integer.parseInt(id));
        if(result>0){
            listCategory(req,resp);
        }


    }


    private void findAllCategory(HttpServletRequest req, HttpServletResponse resp) throws ServletException,IOException {
        resp.setContentType("text/json;charset=UFT-8");
        resp.setCharacterEncoding("UTF-8");
        PrintWriter out = resp.getWriter();
        List<Category> list = categoryService.listCategory("");
        JSONArray jsonArray = JSONArray.parseArray(JSON.toJSONString(list));
        out.print(jsonArray);
        out.flush();
        out.close();

    }








}
